using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class CTPhieuHangTraDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveCTPhieuHangTra(CTPhieuHangTra ctphieuhangtra)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddCTPhieuHangTra", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddCTPhieuHangTra";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctphieuhangtra.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdPhieuHangTra", DbType.Int32, ParameterDirection.Input, ctphieuhangtra.IdPhieuHangTra);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TenHang", DbType.String, ParameterDirection.Input, ctphieuhangtra.TenHang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@MaHang", DbType.String, ParameterDirection.Input, ctphieuhangtra.MaHang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonViTinh", DbType.String, ParameterDirection.Input, ctphieuhangtra.DonViTinh);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonViTinhQuyDoi", DbType.String, ParameterDirection.Input, ctphieuhangtra.DonViTinhQuyDoi);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoLuong", DbType.Int64, ParameterDirection.Input, ctphieuhangtra.SoLuong);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoLuongQuyDoi", DbType.Int64, ParameterDirection.Input, ctphieuhangtra.SoLuongQuyDoi);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonGia", DbType.Int64, ParameterDirection.Input, ctphieuhangtra.DonGia);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ThanhTien", DbType.Int64, ParameterDirection.Input, ctphieuhangtra.ThanhTien);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoKg", DbType.Int64, ParameterDirection.Input, ctphieuhangtra.SoKg);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, ctphieuhangtra.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all CTPhieuHangTra in DB
		public ResultObj GetAllCTPhieuHangTra( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTPhieuHangTra> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTPhieuHangTra", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTPhieuHangTra>();
			CTPhieuHangTra item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTPhieuHangTra();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuHangTra = Convert.ToInt32(reader["IdPhieuHangTra"]);
			item.TenHang = Convert.ToString(reader["TenHang"]);
			item.MaHang = Convert.ToString(reader["MaHang"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.DonViTinhQuyDoi = Convert.ToString(reader["DonViTinhQuyDoi"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.SoLuongQuyDoi = Convert.ToInt64(reader["SoLuongQuyDoi"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
			item.SoKg = Convert.ToInt64(reader["SoKg"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all CTPhieuHangTra in DB with condition
		public ResultObj GetAllCTPhieuHangTraByID(CTPhieuHangTra ctphieuhangtra)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTPhieuHangTra> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTPhieuHangTraByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctphieuhangtra.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTPhieuHangTra>();
			CTPhieuHangTra item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTPhieuHangTra();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuHangTra = Convert.ToInt32(reader["IdPhieuHangTra"]);
			item.TenHang = Convert.ToString(reader["TenHang"]);
			item.MaHang = Convert.ToString(reader["MaHang"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.DonViTinhQuyDoi = Convert.ToString(reader["DonViTinhQuyDoi"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.SoLuongQuyDoi = Convert.ToInt64(reader["SoLuongQuyDoi"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
			item.SoKg = Convert.ToInt64(reader["SoKg"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a CTPhieuHangTra out of DB with condition
		public ResultObj DeleteCTPhieuHangTra(CTPhieuHangTra ctphieuhangtra)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteCTPhieuHangTra", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteCTPhieuHangTra";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctphieuhangtra.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of PhieuHangTra from DB with condition
		public ResultObj GetPhieuHangTra(int IdPhieuHangTra)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuHangTra> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetPhieuHangTra", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdPhieuHangTra);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuHangTra>();
			PhieuHangTra item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuHangTra();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.SoChungTu = Convert.ToString(reader["SoChungTu"]);
			item.NguoiLapPhieu = Convert.ToString(reader["NguoiLapPhieu"]);
			item.KhachHang = Convert.ToString(reader["KhachHang"]);
			item.LyDo = Convert.ToString(reader["LyDo"]);
			item.DiaChi = Convert.ToString(reader["DiaChi"]);
			item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
			item.TongTienHang = Convert.ToInt64(reader["TongTienHang"]);
			item.ChietKhau = Convert.ToInt64(reader["ChietKhau"]);
			item.Thue = Convert.ToInt64(reader["Thue"]);
			item.TongCong = Convert.ToInt64(reader["TongCong"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}

        public ResultObj GetTongHopHangTra()
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<TongHopHangTra> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetTongHopHangTra", CommandType.StoredProcedure);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<TongHopHangTra>();
                TongHopHangTra item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new TongHopHangTra();
                    item.MaPhieu = Convert.ToString(reader["MaSo"]);
                    item.NgayLap = Convert.ToDateTime(reader["NgayThang"]);
                    item.SoChungTu = Convert.ToString(reader["SoChungTu"]);
                    item.NgayChungTu = Convert.ToDateTime(reader["NgayChungTu"]);
                    item.KhachHang = Convert.ToString(reader["KhachHang"]);
                    item.NoiDung = Convert.ToString(reader["NoiDung"]);
                    item.TenHang = Convert.ToString(reader["TenHang"]);
                    item.MaHang = Convert.ToString(reader["MaHang"]);
                    item.DonVi = Convert.ToString(reader["DonViTinhQuyDoi"]);
                    item.SoLuong = Convert.ToInt64(reader["SoLuongQuyDoi"]);
                    item.DonGia = Convert.ToInt64(reader["DonGia"]);
                    item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
                    item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }	
	}
}
